package com.caigou.modules.personal.repository

import com.caigou.comm.base.bean.HttpResult
import com.caigou.comm.base.bean.UserBean
import com.caigou.modules.personal.bean.*
import com.caigou.modules.personal.network.HttpPersonalService
import dagger.hilt.android.scopes.ActivityRetainedScoped
import kotlinx.coroutines.flow.Flow
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import java.io.File
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/09/18
 */
@ActivityRetainedScoped
class PersonalRepository @Inject constructor() {

    fun modifyPwd(userId: String, oldPwd: String, newPwd: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        kotlin.runCatching {
            jsonObject.put("userId", userId)
            jsonObject.put("oldPwd", oldPwd)
            jsonObject.put("newPwd", newPwd)
        }
        val body = jsonObject.toString().toRequestBody()
        return HttpPersonalService.service.modifyPwd(body)
    }

    fun modifyNickName(userId: String, nickName: String): Flow<HttpResult<UserBean>> {
        val jsonObject = JSONObject()
        kotlin.runCatching {
            jsonObject.put("userId", userId)
            jsonObject.put("nickName", nickName)
        }
        val body = jsonObject.toString().toRequestBody()
        return HttpPersonalService.service.modifyNickName(body)
    }

    fun uploadAvatar(userId: String, file: File): Flow<HttpResult<String>> {
        val fileBody = file.asRequestBody("MultipartFile".toMediaTypeOrNull())
        val body = MultipartBody.Builder()
            .addFormDataPart("userId", userId)
            .addFormDataPart("file", file.name, fileBody)
            .build()
        return HttpPersonalService.service.uploadAvatar(body)
    }

    fun modifyRealName(userId: String, realName: String): Flow<HttpResult<UserBean>> {
        val jsonObject = JSONObject()
        kotlin.runCatching {
            jsonObject.put("userId", userId)
            jsonObject.put("realName", realName)
        }
        val body = jsonObject.toString().toRequestBody()
        return HttpPersonalService.service.modifyRealName(body)
    }

    fun modifySignature(userId: String, signature: String): Flow<HttpResult<UserBean>> {
        val jsonObject = JSONObject()
        kotlin.runCatching {
            jsonObject.put("userId", userId)
            jsonObject.put("signature", signature)
        }
        val body = jsonObject.toString().toRequestBody()
        return HttpPersonalService.service.modifySignature(body)
    }

    fun getGrowthValueList(page: Int, limit: Int, userId: String): Flow<HttpResult<GrowthVoBean>> {
        return HttpPersonalService.service.getGrowthValueList(page, limit, userId)
    }

    fun getUserPageInfo(userId: String, currentUserId: String): Flow<HttpResult<UserPageInfoBean>> {
        return HttpPersonalService.service.getUserPageInfo(userId, currentUserId)
    }

    fun getTicketList(type: Int, userId: String): Flow<HttpResult<PostBean>> {
        return HttpPersonalService.service.getTicketList(type, userId)
    }

    fun getFamousList(type: Int, userId: String): Flow<HttpResult<MutableList<FamousItemBean>>> {
        return HttpPersonalService.service.getFamousList(type, userId)
    }

    fun getInfoList(page: Int, limit: Int, userId: String): Flow<HttpResult<InfoBean>> {
        return HttpPersonalService.service.getInfoList(page, limit, userId)
    }

    fun getBoardList(page: Int, limit: Int, boardUserId: String, currentUserId: String): Flow<HttpResult<BoardBean>> {
        return HttpPersonalService.service.getBoardList(page, limit, boardUserId, currentUserId)
    }

    fun setBoardReply(content: String, files: MutableList<File>, senderId: String, receiverId: String, boardUserId: String): Flow<HttpResult<BoardReplyBean>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("content", content)
            .addFormDataPart("senderId", senderId)
            .addFormDataPart("receiverId", receiverId)
            .addFormDataPart("boardUserId", boardUserId)
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile[]".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpPersonalService.service.setReply(body.build())
    }

    fun getBoardDetail(boardUserId: String, replyId: Int): Flow<HttpResult<BoardItemBean>> {
        return HttpPersonalService.service.getBoardDetail(boardUserId, replyId)
    }

    fun setBoardDetailReply(
        content: String,
        files: MutableList<File>,
        parentId: Int,
        senderId: String,
        receiverId: String,
        boardUserId: String
    ): Flow<HttpResult<BoardReplyBean>> {
        val body = MultipartBody.Builder()
            .addFormDataPart("content", content)
            .addFormDataPart("parentId", parentId.toString())
            .addFormDataPart("senderId", senderId)
            .addFormDataPart("receiverId", receiverId)
            .addFormDataPart("boardUserId", boardUserId)
        files.forEach {
            val fileBody = it.asRequestBody("MultipartFile[]".toMediaTypeOrNull())
            body.addFormDataPart("files", it.name, fileBody)
        }
        return HttpPersonalService.service.setReply(body.build())
    }
}